/*
 * Sobel.cpp
 *
 *  Created on: Dec 7, 2009
 *      Author: tptfc
 */

#include "Sobel.h"
#include <math.h>

Sobel::Sobel(int limiar) {
	setLimiar(limiar);
}

Sobel::~Sobel() {
}

void Sobel::applyFilterInVector(int width, int height, int **vectors, int **newVectors) {
	#pragma omp parallel for default(shared)
	for (int i = 0; i < height; i++) {
		#pragma omp parallel for default(shared)
		for (int j = 0; j < width; j++) {
			int tmp;
			int tmpX,tmpY = 0;

			int upLeft = getUpLeftValue(i, j, width, height, vectors);
			int upRight = getUpRightValue(i, j, width, height, vectors);
			int upCenter = getUpCenterValue(i, j, width, height, vectors);
			int centerLeft = getCenterLeftValue(i, j, width, height, vectors);
			int centerRight = getCenterRightValue(i, j, width, height, vectors);
			int downLeft = getDownLeftValue(i, j, width, height, vectors);
			int dowRight = getDownRightValue(i, j, width, height, vectors);
			int dowCenter = getDownCenterValue(i, j, width, height, vectors);


			tmpX= - upLeft- 2 * centerLeft - downLeft + upRight + 2 * centerRight + dowRight;
			tmpY= - upLeft + downLeft - 2 * upCenter + 2 * dowCenter - upRight + dowRight;
			tmp =(int)(sqrt((pow(tmpX,2) + pow(tmpY,2))));

			if (tmp < 0) {
				tmp = 0;
			}

			if (getLimiar() > 255) {
				newVectors[i][j] = tmp;
			} else {
				newVectors[i][j] = (tmp > limiar) ? 255 : 0;
			}
		}
	}
}

void Sobel::setLimiar(int limiar) {
	this->limiar = limiar;
}

int Sobel::getLimiar() {
	return limiar;
}
